#include "adaptivepartitionertest.h"

/*Registry this TestSuite to theTestFactoryRegistry*/
CPPUNIT_TEST_SUITE_REGISTRATION (AdaptivePartitionerTest);

void AdaptivePartitionerTest::setUp() {
	SG_init();
}

void AdaptivePartitionerTest::tearDown() {
	SG_clear();
}

void AdaptivePartitionerTest::test_update_speed_vector() { fprintf(stderr,">>> AdaptivePartitionerTest::test_update_speed_vector()\n");
	AdaptivePartitioner *partitioner = new AdaptivePartitioner(10000, 20000, 0.05, 0.8, 0.15, 5);
	Job *job = new Job("job_1", 1000, 1000000);
	Grid *grid = new Grid("test");
	partitioner->configure_workload(11000000, job, grid);
	
	for(int i = 0; i < partitioner->speed_window; i++)
		CPPUNIT_ASSERT_MESSAGE("Number of speed tasks should be -1!", partitioner->speed_vector[i] == -1);

	partitioner->update_speed_vector(100);
	CPPUNIT_ASSERT_MESSAGE("Number of 1st speed task should be 100!", partitioner->speed_vector[0] == 100);
	
	partitioner->update_speed_vector(200);
	CPPUNIT_ASSERT_MESSAGE("Number of 2nd speed task should be 200!", partitioner->speed_vector[1] == 200);
	
	partitioner->update_speed_vector(300);
	CPPUNIT_ASSERT_MESSAGE("Number of 3rd speed task should be 300!", partitioner->speed_vector[2] == 300);
	
	partitioner->update_speed_vector(400);
	CPPUNIT_ASSERT_MESSAGE("Number of 4th speed task should be 400!", partitioner->speed_vector[3] == 400);	
	
	partitioner->update_speed_vector(500);
	CPPUNIT_ASSERT_MESSAGE("Number of 5th speed task should be 500!", partitioner->speed_vector[4] == 500);	
	
	partitioner->update_speed_vector(600);
	CPPUNIT_ASSERT_MESSAGE("Number of 5th speed task should be 600!", partitioner->speed_vector[4] == 600);
	CPPUNIT_ASSERT_MESSAGE("Number of 4th speed task should be 500!", partitioner->speed_vector[3] == 500);
	CPPUNIT_ASSERT_MESSAGE("Number of 3rd speed task should be 400!", partitioner->speed_vector[2] == 400);
	CPPUNIT_ASSERT_MESSAGE("Number of 2nd speed task should be 300!", partitioner->speed_vector[1] == 300);
	CPPUNIT_ASSERT_MESSAGE("Number of 1st speed task should be 200!", partitioner->speed_vector[0] == 200);
	
	partitioner->update_speed_vector(700);
	CPPUNIT_ASSERT_MESSAGE("Number of 5th speed task should be 700!", partitioner->speed_vector[4] == 700);
	CPPUNIT_ASSERT_MESSAGE("Number of 4th speed task should be 600!", partitioner->speed_vector[3] == 600);
	CPPUNIT_ASSERT_MESSAGE("Number of 3rd speed task should be 500!", partitioner->speed_vector[2] == 500);
	CPPUNIT_ASSERT_MESSAGE("Number of 2nd speed task should be 400!", partitioner->speed_vector[1] == 400);
	CPPUNIT_ASSERT_MESSAGE("Number of 1st speed task should be 300!", partitioner->speed_vector[0] == 300);

	delete job;
	delete partitioner;
	
}

void AdaptivePartitionerTest::test_update_granularity() { fprintf(stderr,">>> AdaptivePartitionerTest::test_update_granularity()\n");
	AdaptivePartitioner *partitioner = new AdaptivePartitioner(100000, 200000, 0.05, 0.8, 0.15, 5);
	Job *job = new Job("job_1", 1000, 1000000);
	Grid *grid = new Grid("test");
	partitioner->configure_workload(10000000, job, grid);
	
	long double initial = 100000;
	long double duplicated = 200000;
	long double quadruplicated = 400000;
	
	CPPUNIT_ASSERT_MESSAGE("Test 1", partitioner->granularity_current == initial);

	partitioner->update_speed_vector(100);
	partitioner->update_granularity(initial);
	CPPUNIT_ASSERT_MESSAGE("Test 2", partitioner->granularity_current == initial);
	
	partitioner->update_speed_vector(200);
	partitioner->update_granularity(initial);
	CPPUNIT_ASSERT_MESSAGE("Test 3", partitioner->granularity_current == initial);
	
	partitioner->update_speed_vector(300);
	partitioner->update_granularity(initial);
	CPPUNIT_ASSERT_MESSAGE("Test 4", partitioner->granularity_current == initial);
	
	partitioner->update_speed_vector(400);
	partitioner->update_granularity(initial);
	CPPUNIT_ASSERT_MESSAGE("Test 5", partitioner->granularity_current == initial);
	
	partitioner->update_speed_vector(500);
	partitioner->update_granularity(initial);
	CPPUNIT_ASSERT_MESSAGE("Test 6", partitioner->granularity_current == initial);
	
	partitioner->update_speed_vector(600);
	partitioner->update_granularity(initial);
	CPPUNIT_ASSERT_MESSAGE("Test 7", partitioner->granularity_current == initial);
	
	partitioner->update_speed_vector(700);
	partitioner->update_granularity(duplicated);
	CPPUNIT_ASSERT_MESSAGE("Test 7.1", partitioner->granularity_current == duplicated);
	
	partitioner->update_speed_vector(6000000);
	partitioner->update_granularity(quadruplicated);
	CPPUNIT_ASSERT_MESSAGE("Test 8", partitioner->granularity_current == quadruplicated);
	
	partitioner->update_speed_vector(1);
	partitioner->update_speed_vector(1);
	partitioner->update_speed_vector(1);
	partitioner->update_speed_vector(1);
	partitioner->update_speed_vector(1);
	partitioner->update_speed_vector(1);
	partitioner->update_granularity(duplicated);
	CPPUNIT_ASSERT_MESSAGE("Test 9", partitioner->granularity_current == (quadruplicated + duplicated) / 2 );
	
	duplicated = (quadruplicated + duplicated) / 2;
	
	partitioner->update_speed_vector(1000);
	partitioner->update_speed_vector(1000);
	partitioner->update_speed_vector(1000);
	partitioner->update_speed_vector(1000);
	partitioner->update_speed_vector(1000);
	partitioner->update_granularity(duplicated);
	CPPUNIT_ASSERT_MESSAGE("Test 10", partitioner->granularity_current == (quadruplicated + duplicated) / 2 );
	
	delete job;
	delete partitioner;
	
}
